package cn.xgl6.springcloud.order.controller;

import cn.xgl6.springcloud.domain.Order;
import cn.xgl6.springcloud.domain.Product;
import cn.xgl6.springcloud.order.service.OrderService;
import cn.xgl6.springcloud.order.service.ProductService;
import cn.xgl6.springcloud.utils.CommonResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author xgl
 * @create 2020-05-14 15:33
 */
@RestController
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private ProductService productService;

    /*@PostMapping("/order/create")
    public CommonResult<Integer> createOrder(Order order) {
        log.info("接收到的参数为{}",order);
        //调用商品微服务,查询商品信息
        CommonResult commonResult = restTemplate.getForObject("http://localhost:8081/product/get/" + order.getPid(), CommonResult.class);
        log.info("查询到commonResult的信息为{}",commonResult);
        Product product = JSON.parseObject(JSON.toJSONString(commonResult.getData()), Product.class);
        log.info("类型强转{}",product);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        int id = orderService.createOrder(order);
        if (id != 0){
            return new CommonResult<>(200,"success",id);
        }else {
            return new CommonResult<>(500,"订单创建失败",id);
        }
    }*/
    /*@PostMapping("/order/create")
    public CommonResult<Integer> createOrder(Order order) {
        log.info("接收到的参数为{}",order);
        //调用商品微服务,查询商品信息
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance instance = instances.get(0);
        String url = "service-product";
        url = "http://" + url + "/product/get/" + order.getPid();
        CommonResult commonResult = restTemplate.getForObject(url, CommonResult.class);
        log.info("查询到commonResult的信息为{}",commonResult);
        Product product = JSON.parseObject(JSON.toJSONString(commonResult.getData()), Product.class);
        log.info("类型强转{}",product);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        int id = orderService.createOrder(order);
        if (id != 0){
            return new CommonResult<>(200,"success",id);
        }else {
            return new CommonResult<>(500,"订单创建失败",id);
        }
    }*/
    /*@PostMapping("/order/create")
    public CommonResult<Integer> createOrder(Order order) {
        log.info("接收到的参数为{}",order);
        //调用商品微服务,查询商品信息
        CommonResult commonResult = productService.getProductById(order.getPid());
        log.info("查询到commonResult的信息为{}",commonResult);
        Product product = JSON.parseObject(JSON.toJSONString(commonResult.getData()), Product.class);
        log.info("类型强转{}",product);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        int id = orderService.createOrder(order);
        if (id != 0){
            return new CommonResult<>(200,"success",id);
        }else {
            return new CommonResult<>(500,"订单创建失败",id);
        }
    }*/
    @PostMapping("/order/create")
    public CommonResult<String> createOrder(Order order) {
        log.info("接收到的参数为{}",order);
        //调用商品微服务,查询商品信息
        CommonResult commonResult = productService.getProductById(order.getPid());
        log.info("查询到commonResult的信息为{}",commonResult);
        Product product = JSON.parseObject(JSON.toJSONString(commonResult.getData()), Product.class);
        log.info("类型强转{}",product);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        //模拟响应时间
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //int id = orderService.createOrder(order);
        return new CommonResult<>(200,"success","插入成功");
    }

    @GetMapping("/order/message1")
    public CommonResult<String> getMeg1(){
        return new CommonResult<>(200,"success","测试高并发成功1");
    }
    @GetMapping("order/message2")
    public CommonResult<String> getMeg2(){
        return new CommonResult<>(200,"success","测试高并发成功2");
    }
}
